package com.hongdee.crm.workitem.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.crm.workitem.service.ITCrmCarWorkItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 名  称:TCrmCarWorkItemController
 * 类  型: class
 * 描  述:
 * 路  径:
 * 作  者: 术洪超
 * 日  期:2017-02-22 17:49
 */
@RestController
@RequestMapping("/crm/workitem")
public class TCrmCarWorkItemController {

    @Autowired
    private ITCrmCarWorkItemService itCrmCarWorkItemService;

    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param start 起始记录
     * @param limit 页面大小
     * @return Map
     * @throws Exception
     * @desc 通过 JDBCDao分页查询
     * @author 术洪超
     */

    @RequestMapping("/impPage")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "") String start,
                    @RequestParam(name = "limit", defaultValue = "") String limit
    ) throws Exception {

        Map map = itCrmCarWorkItemService.impPage(uid, start, limit);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;

    }

    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @param id    要删除数据的主键
     * @return Map
     * @throws Exception
     * @desc
     * @author 术洪超
     */
    @RequestMapping("/impDel")
    @ResponseBody
    public ResultEntity impDel(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token,
                               @RequestParam(name = "id", defaultValue = "") String id)
            throws Exception {
        itCrmCarWorkItemService.impDel(id);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("删除数据成功！");
        return res;
    }

    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @param id    要确认数据的主键
     * @return Map
     * @throws Exception
     * @desc
     * @author 术洪超
     */
    @RequestMapping("/impConfirm")
    @ResponseBody
    public ResultEntity impConfirm(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token,
                                   @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        itCrmCarWorkItemService.impConfirm(id, uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("数据确认成功！");
        return res;
    }
    /**
     * @param data 批量数据
     * @return Map
     * @throws Exception
     * @desc   维修工单批量确认
     * @author 徐钦勇
     */
    @RequestMapping("/impConfirms")
    @ResponseBody
    public ResultEntity impConfirms(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token,
                                  @RequestBody List<Map> data) throws Exception {
        itCrmCarWorkItemService.multiImpConfirm(data, uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("数据确认成功！");
        return res;
    }
    /**
     * @return Map
     * @throws Exception
     * @desc   维修工单全部确认
     * @author 徐钦勇
     */
    @RequestMapping("/impConfirmAll")
    @ResponseBody
    public ResultEntity impConfirmAll(@RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token) throws Exception {
        itCrmCarWorkItemService.impConfirmAll(uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("数据确认成功！");
        return res;
    }
    /**
     * @param uid 登录用户id
     * @param orderCode 工单号
     * @return List
     * @throws Exception
     * @desc   根据工单号和公司查询工单项目
     * @author 徐钦勇
     */
    @RequestMapping("/page")
    @ResponseBody
    public ResultEntity page (@RequestParam(name = "uid", defaultValue = "") String uid,
                      @RequestParam(name = "token", defaultValue = "") String token,
                      @RequestParam(name = "orderCode", defaultValue = "")String orderCode)throws Exception{
        ResultEntity res = new ResultEntity();
        res.setDataList(this.itCrmCarWorkItemService.page(uid,orderCode));
        res.setSuccess(true);
        res.setMsg("查询成功！");
        return res;
    }


    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc 导入删除
     * @author 术洪超
     */
    @RequestMapping("/impDelAll")
    @ResponseBody
    public ResultEntity impDelAll(@RequestParam(name = "uid", defaultValue = "") String uid,
                                  @RequestParam(name = "token", defaultValue = "") String token) throws Exception {
        this.itCrmCarWorkItemService.impDelAll(uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("删除成功！");
        return res;
    }


    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc 删除多条待导入的数据
     * @author 术洪超
     */
    @RequestMapping("/impDels")
    @ResponseBody
    public ResultEntity impDels(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token,
                                @RequestBody List<Map<Object, String>> data) throws Exception {
        if (org.springframework.util.StringUtils.isEmpty(data)) {
            throw new Exception("请选择记录！");
        }
        this.itCrmCarWorkItemService.impDels(data, uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("删除成功！");
        return res;
    }

    @RequestMapping("/workItemQuery")
    @ResponseBody
    public Map workItemQuery(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token,
                                   @RequestParam(name = "start", defaultValue = "0") String start,
                                   @RequestParam(name = "limit", defaultValue = "50") String limit,
                                   @RequestBody Map data) throws Exception {
        //String cid = TokenUtil.getCompanyId(uid);
        Map map = this.itCrmCarWorkItemService.workItemQuery(uid,data,start,limit);
        map.put("success", true);
        map.put("msg", "查询成功！");
        return map;
    }


}
